package com.jman.front.controller;


import cn.hutool.core.bean.BeanUtil;
import com.jman.front.clients.*;
import com.jman.front.dto.*;
import com.jman.front.util.InfoClient;
import org.jetbrains.annotations.NotNull;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.net.SocketTimeoutException;
import java.util.List;

@RestController
@RequestMapping("/req")
public class RequestController {
    @Resource
    private LoginClient loginClient;
    @Resource
    private RegisterClient registerClient;
    @Resource
    private MessageClient messageClient;
    @Resource
    private ReplyClient replyClient;
    @Resource
    private AnnouncementClient announcementClient;
    @Resource
    private ResidentClient residentClient;
    @Resource
    private StringRedisTemplate stringRedisTemplate;


    @PostMapping("/login")
    public Result login(@RequestBody LoginFormDTO loginFormDTO){
        return Agent.login(loginClient, loginFormDTO);
    }

    @PostMapping("/register")
    public Result register(@RequestBody RegisterFormDTO registerFormDTO){
        return Agent.register(registerClient, registerFormDTO);
    }

    @GetMapping("/getInfo")
    public Result getToken(HttpServletRequest request){
        UserDTO userDTO = InfoClient.getUserInfo(request, stringRedisTemplate);
        return Result.ok(userDTO);
    }

    @DeleteMapping("/outLogin")
    public Result outLogin(HttpServletRequest request){
        String token = null;
        try {
            token = request.getHeader("authorization");
            stringRedisTemplate.delete("login:user:" + token);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
        return Result.ok();
    }

    @PostMapping("/message/saveInfo")
    public Result saveMessage(@RequestBody @NotNull MessageDTO messageDTO, HttpServletRequest request){
        UserDTO userDTO = InfoClient.getUserInfo(request, stringRedisTemplate);
        assert userDTO != null;
        messageDTO.setSenderId(userDTO.getId());
        String message = Agent.saveMessage(messageClient, messageDTO);
        return Result.ok(message);
    }

    @GetMapping("/message/getInfo")
    public Result getMessage(){
        List<Message> message = Agent.getMessage(messageClient);
        if (message == null) {
            return Result.fail("留言获取失败！");
        }
        return Result.ok(message);
    }

    @DeleteMapping("/message/delInfo/{id}")
    public Result delMessage(@PathVariable Integer id){
        if (Agent.delMessage(messageClient, id)) {
            return Result.ok();
        }
        return Result.fail("留言删除失败!");
    }

    @GetMapping("/reply/getReply")
    public Result getReply(HttpServletRequest request){
        UserDTO userDTO = InfoClient.getUserInfo(request, stringRedisTemplate);
        assert userDTO != null;
        List<ReplyToUser> reply = Agent.getReply(replyClient, userDTO.getId());
        return Result.ok(reply);
    }

    @PostMapping("/reply/saveReply")
    public Result saveReply(HttpServletRequest request, @RequestBody ReplyDTO replyDTO){
        UserDTO userDTO = InfoClient.getUserInfo(request, stringRedisTemplate);
        Reply reply = BeanUtil.copyProperties(replyDTO, Reply.class);
        if (Agent.saveReply(replyClient, reply, userDTO.getId())) {
            return Result.ok();
        }
        return Result.fail("回复发送失败!");
    }

    @GetMapping("/announcement/{id}")
    public Result getAnnouncementById(@PathVariable("id") Integer id){
        Announcement announcement = Agent.getAnnouncementById(announcementClient, id);
        if (announcement == null) {
            return Result.fail("公告查询失败!");
        }
        return Result.ok(announcement);
    }

    @GetMapping("/announcement/parts")
    public Result getPartsAnnouncement(){
        List<AnnouncementDTO> announcementDTOS = Agent.getPartsAnnouncement(announcementClient);
        if (announcementDTOS == null) {
            return Result.fail("公告查询失败！");
        }
        return Result.ok(announcementDTOS);
    }

    @PostMapping("/announcement/save")
    public Result saveAnnouncement(HttpServletRequest request, @RequestBody AnnouncementInfo announcementInfo){
        UserDTO userDTO = InfoClient.getUserInfo(request, stringRedisTemplate);
        Announcement announcement = BeanUtil.copyProperties(announcementInfo, Announcement.class);
        announcement.setSenderId(userDTO.getId());
        if (Agent.saveAnnouncement(announcementClient, announcement)) {
            return Result.ok();
        }
        return Result.fail("发布公告信息失败！");
    }

    @GetMapping("/announcement/all")
    public Result getAllAnnouncement(){
        if (Agent.getAllAnnouncement(announcementClient) == null) {
            return Result.fail("获取公告信息失败！");
        }
        return Result.ok();
    }

    @DeleteMapping("/announcement/del/{id}")
    public Result delAnnouncement(@PathVariable Integer id){
        if (Agent.delAnnouncement(announcementClient, id)) {
            return Result.ok();
        }
        return Result.fail("公告删除失败！");
    }

    @PostMapping("/resident/getPage")
    public Result getResidentByPage(@RequestBody PageInfo pageInfo){
        PageBean<Resident> page = Agent.getPage(residentClient, pageInfo);
        if (page == null) {
            return Result.fail("用户信息查询失败!");
        }
        return Result.ok(page);
    }

    @PutMapping("/resident/putInfo")
    public Result updateResident(@RequestBody Resident resident){
        if (Agent.updateResident(residentClient, resident)) {
            return Result.ok();
        }
        return Result.fail("住户信息修改失败!");
    }

    @DeleteMapping("/resident/delInfo/{id}")
    public Result delResidentById(@PathVariable("id") Integer id){
        if (Agent.delInfoById(residentClient, id)) {
            return Result.ok();
        }
        return Result.fail("住户删除修改失败!");
    }
}
